<?php


namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Admin\AdminClassController;
use App\Http\Controllers\Admin\LogoController;
use App\Http\Controllers\Business\BusinessClassificationController;
use App\Http\Controllers\Business\BusinessInfoController;
use App\Http\Controllers\Business\BusinessTimeController;
use App\Http\Controllers\Business\FoodCategoryController;
use App\Http\Controllers\Business\FoodClassificationController;
use App\Http\Controllers\Business\FoodInfoController;

use App\Http\Controllers\Order\BusinessCartController;
use App\Http\Controllers\Order\FlavorController;
use App\Http\Controllers\Order\OrderBecauseController;
use App\Http\Controllers\User\AddressController;
use App\Http\Controllers\Order\BonusController;
use App\Http\Controllers\Order\BonusRecordController;
use App\Http\Controllers\Order\CartController;
use App\Http\Controllers\Order\OrdersController;
use App\Http\Controllers\User\CollectionController;
use App\Http\Controllers\User\UserInfoController;
use App\Models\Admin\AdminClass;
use App\Models\Business\FoodCategory;
use App\Models\User\BonusRecord;
use App\Models\User\Collection;
use Illuminate\Http\Request;

/**
 * Class Access
 * 权限控制描述文件
 * 用户通过所属用户的user_type来确定用户的类型
 * 通过用户的类型来查看这个用户是否有对这个函数或者class有相应的权限
 * 权限细分到函数
 * @package App\Http\Controllers\Auth
 */
class Access
{
    private $auth = array();
    private $patient = array();
    private $business = array();
    private $admin = array();
    private $record = array();
    private $exempt = array();
    public function __construct()
    {
        $this->exempt = array(
            [FoodCategoryController::class,['index','show']],//查看所有商品类目
            [BusinessInfoController::class,['index','show']],//查看所有商家
            [BusinessClassificationController::class,['index','show']],//查看所有商家分类
            [LogoController::class,['index']], //logo
            [OrderBecauseController::class,['index','show']] //查看失败原因
        );

        $this->business = array(
            [BusinessInfoController::class,['index','store','update','show']],//查看所有商家
            [FoodInfoController::class,['index','store','update','show','destroy']],//查看所有商品
            [BusinessClassificationController::class,['index','show']],//查看所有商家分类
            [FoodClassificationController::class,['index','store','show']],//查看所有商品分类
            [FoodCategoryController::class,['index','show']],//查看所有商品类目
            [OrderBecauseController::class,['index','show']], //查看失败原因
            [BusinessCartController::class,['index','show','store','destroy','alldelete']],//商家购物车查看
            [BusinessTimeController::class,['index','store','update','show','destroy']],//查看所有商家营业时间
            [OrdersController::class,['index','show','store','update','destroy']],//订单查看
            [FlavorController::class,['index','show','store','update','destroy']]  //口味选择
        );//商家的权限
        //TODO 从文件中读取这个权限表
        $this->admin = array(
            [BusinessInfoController::class,['index','store','update','show','destroy','alldelete']],//查看所有商家
            [FoodInfoController::class,['index','store','update','show','destroy','alldelete']],//查看所有商品
            [UserInfoController::class,['index','store','update','show','destroy','alldelete']],//查看所有用户
            [BusinessClassificationController::class,['index','store','update','show','destroy']],//查看所有商家分类
            [FoodClassificationController::class,['index','store','update','show','destroy']],//查看所有商品分类
            [BusinessTimeController::class,['index','store','update','show','destroy']],//查看所有商家营业时间
            [FoodCategoryController::class,['index','store','update','show','destroy']],//查看所有商品类目
            [OrderBecauseController::class,['index','store','update','show','destroy']], //查看失败原因
            [LogoController::class,['index','update']], //logo
            [BonusController::class,['index','show']],//优惠券查看
            [BonusRecordController::class,['index','show']],//优惠券历史查看
            [OrdersController::class,['index','show','update','destroy']],//订单查看 修改
            [AdminClassController::class,['index','show','store','update','destroy']]//后台地址管理
        );

        $this->patient = array(
            [BusinessInfoController::class,['index','show']],//查看商家
            [FoodInfoController::class,['index','show']],//查看商品
            [AddressController::class,['index','store','update','show','destroy','showone','showstatus']],//查看地址
            [UserInfoController::class,['index','update','show']],//查看所有用户
            [CollectionController::class,['index','show','store','update','destroy']],  //查看用户收藏
            [BusinessClassificationController::class,['index','show']],//查看所有商家分类
            [FoodClassificationController::class,['index','show','search']],//查看所有商品分类
            [OrderBecauseController::class,['index','show']], //查看失败原因
            [CartController::class,['index','store','update','show','destroy','alldelete']],//购物车 用户的权限
            [BonusController::class,['index','show']],//优惠券查看
            [BonusRecordController::class,['index','show']],//优惠券历史查看
            [OrdersController::class,['index','show','store','update','destroy']],//订单查看和添加
        ); //病人权限

        $this->record = array(
            'admin' =>$this->admin,
            'patient'=>$this->patient,
            'business'=>$this->business
        );
        //把每个user type做映射
        $this->auth = array(
            1=>'patient',
            2=>'business',
            3=>'admin'
        );

    }

    /**
     * 验证此次的session是否有权限操作这个接口
     * 主要逻辑是通过request中的user_type来确定
     * @package App\Http\Controllers\Auth
     */
    public function isAuth(Request $request){
        try{
            $user_type = $request->user_type;
        }catch (\Exception $e)
        {
            return false;
        }

        $info = $request->route()->action;
        $controller = $info['controller'];
        $controllerArr = explode('@',$controller);
        $cls_name = $controllerArr[0];
        $method_name = (array) $controllerArr[1];
        foreach ($this->exempt as $value){
            foreach ($value[1] as $func){
                if($cls_name === $value[0] and $method_name[0] === $func)
                    return true;
            }
        }
        try{
           $type_name = $this->auth[$user_type];
        }catch (\Exception $e){
            return false;
        }
        $auths = $this->record[$type_name];
        foreach ($auths as $v){
            foreach ($v[1] as $name) {
            if(($v[0] === $cls_name) and ($name === $method_name[0]))
                return true;
            }
        };
        return false;
    }

}
